package com.shell.customer.accreditation.rx;

import com.shell.customer.accreditation.AccreditEvent;
import com.shell.customer.accreditation.vo.AccreditationVo;
import com.shell.customer.base.service.CusExtraService;
import com.shell.customer.po.CusExtra;
import com.shell.customer.po.CusRec;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SetCreditLineSubscriber extends OnErrorNotImplementedSubscriber<AccreditEvent> {
	@Autowired
	private CusExtraService pubCusExtraService;

	public void onApplicationEvent(AccreditEvent event) {
		AccreditationVo accreditationVo = event.getAccreditationVo();
		if (accreditationVo.getCreditLine() == null) {
			return;
		}
		CusRec cus = accreditationVo.getCustomerC();
		CusExtra cusExtra = (CusExtra) this.pubCusExtraService.selectByKey(cus.getCusId());
		if (null == cusExtra) {
			cusExtra = new CusExtra();
			cusExtra.setCusId(cus.getCusId());
			cusExtra.setCreditLine(accreditationVo.getCreditLine());

			this.pubCusExtraService.save(cusExtra);
		} else {
			cusExtra.setCusId(cus.getCusId());
			cusExtra.setCreditLine(accreditationVo.getCreditLine());
			this.pubCusExtraService.updateNotNull(cusExtra);
		}
	}
}
